import { mainRouter } from '@/router/index';
const state = {
    pageOpenedList: [],
    tagsList: [...mainRouter.children],
    currentPageName: ''
}

const getters = {
    pageOpenedList: () => state.pageOpenedList,
    currentPageName: () => state.currentPageName

}

const actions = {
    recordPageTags({ commit }, active) {
        let pageOpenedList = state.pageOpenedList
        let openedPageLen = pageOpenedList.length;
        let i = 0;
        let tagHasOpened = false;
        while (i < openedPageLen) {
            if (active === pageOpenedList[i].name) { // 页面已经打开
                tagHasOpened = true;
                break;
            }
            i++;
        }
        if (!tagHasOpened) {
            let tag = state.tagsList.filter((item) => {
                if (item.children.length > 0) {
                    return active === item.children[0].name;
                } else {
                    return active === item.name;
                }
            });
            tag = tag[0];
            tag = tag.children.length > 0 ? tag.children[0] : tag;
            commit('increateTag', tag);

        }

    },
    //打开详情页面 标签
    recordDetailPageTags({ commit }, tagObj) {
        let pageOpenedList = state.pageOpenedList
        let openedPageLen = pageOpenedList.length;
        let i = 0;
        let tagHasOpened = false;
        let path = tagObj.path
        while (i < openedPageLen) {
            if (path === pageOpenedList[i].path) { // 页面已经打开
                tagHasOpened = true;
                break;
            }
            i++;
        }
        if (!tagHasOpened) {
            commit('increateTag', tagObj);
        }

    },
    removePageTags({ commit }, tagObj) {
        let pageOpenedList = state.pageOpenedList
        let openedPageLen = pageOpenedList.length;
        let i = 0;
        let tagHasOpened = false;
        let path = tagObj.path
        while (i < openedPageLen) {
            if (pageOpenedList[i].path.indexOf(path) > -1) { // 页面已经打开
                tagHasOpened = true;
                break;
            }
            i++;
        }

        if (tagHasOpened) {
            commit('removeIncludePathTag', path);
        }
    }

}
const mutations = {
    increateTag(state, tagObj) {
        state.pageOpenedList.push(tagObj);
        localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList); // 本地存储已打开页面
        // state.pageOpenedList.splice(0, 0, tagObj);
    },
    removeTag(state, name) {
        state.pageOpenedList.map((item, index) => {
            if (item.name === name) {
                state.pageOpenedList.splice(index, 1);
            }
        });
        localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);

    },
    removeIncludePathTag(state, path) {
        state.pageOpenedList.map((item, index) => {
            if (item.path.indexOf(path) > -1) {
                state.pageOpenedList.splice(index, 1);
            }
        });
        localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);

    },
    removePathTag(state, path) {
        state.pageOpenedList.map((item, index) => {
            if (item.path === path) {
                state.pageOpenedList.splice(index, 1);
            }
        });
        localStorage.pageOpenedList = JSON.stringify(state.pageOpenedList);

    },
    getOpenedList(state) {
        state.pageOpenedList = localStorage.pageOpenedList ? JSON.parse(localStorage.pageOpenedList) : [mainRouter.children[0]];
    },
    setCurrentPageName(state, name) {
        state.currentPageName = name;
    }

}
export default {
    state,
    getters,
    actions,
    mutations
}